﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using System.Linq;

namespace helpcarla
{
    public partial class frmMain : Form
    {
        static string connectionstring = "Server=74.208.102.196;Port=3306;Database=carla;Uid=franklin;Pwd=frklin;";
        static MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connectionstring);
        static bool isConnected;

        public frmMain()
        {
            InitializeComponent();
        }

        public static MySql.Data.MySqlClient.MySqlConnection connectDB()
        {
            bool success = true;

            if (isConnected == false)
            {
                try
                {
                    conn.Open();
                }
                catch (MySql.Data.MySqlClient.MySqlException mysqlEX)
                {
                    isConnected = false;
                    success = false;
                    Console.WriteLine("You have received an error while connecting to the database: " + mysqlEX.Message);
                }
                if (success)
                {
                    isConnected = true;
                }
            }
            return conn;
        }

        public static void disconnectDB()
        {
            if (isConnected)
            {
                conn.Close();
            }
        }

        public bool IsConnected()
        {
            return isConnected;
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            frmDailyLog foodLog = new frmDailyLog();
            foodLog.Show();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            string today = DateTime.Today.ToString("yyyy-MM-dd");
            string selectQuery = "SELECT Date FROM DailyFoodLog WHERE Date < " + today;
            string todayQuery = "SELECT Date FROM DailyFoodLog WHERE Date = " + today;
            DataSet ds = new DataSet("DailyFoodLogDataSet");
            DataSet dsToday = new DataSet("DailyFoodLogTodayDataSet");
            try
            {
                connectDB();
                MySql.Data.MySqlClient.MySqlDataAdapter da = new MySql.Data.MySqlClient.MySqlDataAdapter(selectQuery, connectDB());
                da.Fill(ds, "DailyFoodLog");
                MySql.Data.MySqlClient.MySqlDataAdapter daToday = new MySql.Data.MySqlClient.MySqlDataAdapter(todayQuery, connectDB());
                daToday.Fill(dsToday, "DailyFoodLog");
                if (dsToday.Tables["DailyFoodLog"].Rows.Count < 1)
                {
                    btnYes.Enabled = false;
                }
                foreach (DataRow dr in ds.Tables["DailyFoodLog"].Rows)
                {
                    DateTime boldDay = new DateTime();
                    boldDay = Convert.ToDateTime(dr.ToString());
                    MonthlyCalendar.AddBoldedDate(boldDay);
                }
                disconnectDB();
            }
            catch (MySql.Data.MySqlClient.MySqlException mysqlex)
            {
                MessageBox.Show("There was an error while trying to connect to the database: " + mysqlex.Message);
                disconnectDB();
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show("There has been an invalid operation: " + ioe.Message);
                disconnectDB();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutThisProgramToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout about = new frmAbout();
            about.Show();
        }

        private void symptomsSummaryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSymptoms s = new frmSymptoms();
            s.Show();
        }

        private void sendFeedbackToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmFeedback f = new frmFeedback();
            f.Show();
        }

        private void emailMedicalInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmEmailInfo email = new frmEmailInfo();
            email.Show();
        }

        private void viewMedicalInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmViewMedicalInfo viewmi = new frmViewMedicalInfo();
            viewmi.Show();
        }

        private void printPreiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            printPreviewDialog.ShowDialog();
        }

        private void printMedicalInfoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            printDialog.ShowDialog();
        }

        private void medicalDictionaryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmMedicalDictionary md = new frmMedicalDictionary();
            md.Show();
        }

        private void fAQToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmFAQ faq = new frmFAQ();
            faq.Show();
        }

        private void mealsInfoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmDailyLog dl = new frmDailyLog();
            dl.Show();
            dl.tabcontrolDailyLog.SelectTab("tabMealInfo");
        }

        private void sleepHabitsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDailyLog dl = new frmDailyLog();
            dl.tabcontrolDailyLog.SelectTab("tabSleepHabits");
            dl.Show();
        }

        private void emotionalLevelsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDailyLog dl = new frmDailyLog();
            dl.tabcontrolDailyLog.SelectTab("tabEmotionalLevels");
            dl.Show();
        }

        private void moodChangesToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmDailyLog dl = new frmDailyLog();
            dl.tabcontrolDailyLog.SelectTab("tabMoodChanges");
            dl.Show();
        }
    }
}
